<template>
  <div style="width: 1200px;height: 100vh;margin: 0 auto;">
    <h2>异常考勤记录列表</h2>
    <span style="display: flex;justify-content: space-between;">
      <router-link to="/main/renshi/kaoqing/kqguanli/kqyichanggl" style="">返回</router-link>
    <router-link to="/main/renshi/kaoqing/kqguanli/kqyichanggl"
      style="">新增记录</router-link>
    </span>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="employeeId" label="工号" width="120"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="workGroup" label="班组" width="120"></el-table-column>
      <el-table-column prop="date" label="日期" width="120">
        <template slot-scope="scope">
          {{ formatDate(scope.row.date) }}
        </template>
      </el-table-column>
      <el-table-column prop="lateCount" label="迟到次数" width="100"></el-table-column>
      <el-table-column prop="earlyLeaveCount" label="早退次数" width="100"></el-table-column>
      <el-table-column prop="absenteeismCount" label="旷工次数" width="100"></el-table-column>
      <el-table-column prop="regularAttendanceTime" label="正班出勤时间" width="150"></el-table-column>
      <el-table-column prop="abnormalReason" label="异常原因" width="200"></el-table-column>
      <el-table-column label="操作" width="250"fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 详情对话框 -->
    <el-dialog title="异常考勤详情" :visible.sync="dialogVisible" width="70%">
      <div v-if="detailData">
        <el-descriptions :column="3" border>
          <el-descriptions-item label="工号">{{ detailData.employeeId }}</el-descriptions-item>
          <el-descriptions-item label="姓名">{{ detailData.name }}</el-descriptions-item>
          <el-descriptions-item label="班组">{{ detailData.workGroup }}</el-descriptions-item>
          <el-descriptions-item label="日期">{{ formatDate(detailData.date) }}</el-descriptions-item>
          <el-descriptions-item label="班一上班">{{ detailData.shift1Start }}</el-descriptions-item>
          <el-descriptions-item label="班一下班">{{ detailData.shift1End }}</el-descriptions-item>
          <el-descriptions-item label="班二上班">{{ detailData.shift2Start }}</el-descriptions-item>
          <el-descriptions-item label="班二下班">{{ detailData.shift2End }}</el-descriptions-item>
          <el-descriptions-item label="班三上班">{{ detailData.shift3Start }}</el-descriptions-item>
          <el-descriptions-item label="班三下班">{{ detailData.shift3End }}</el-descriptions-item>
          <el-descriptions-item label="附加班一上班">{{ detailData.extraShift1Start }}</el-descriptions-item>
          <el-descriptions-item label="附加班一下班">{{ detailData.extraShift1End }}</el-descriptions-item>
          <el-descriptions-item label="附加班二上班">{{ detailData.extraShift2Start }}</el-descriptions-item>
          <el-descriptions-item label="附加班二下班">{{ detailData.extraShift2End }}</el-descriptions-item>
          <el-descriptions-item label="附加班三上班">{{ detailData.extraShift3Start }}</el-descriptions-item>
          <el-descriptions-item label="附加班三下班">{{ detailData.extraShift3End }}</el-descriptions-item>
          <el-descriptions-item label="迟到时间">{{ detailData.lateTime }}</el-descriptions-item>
          <el-descriptions-item label="迟到次数">{{ detailData.lateCount }}</el-descriptions-item>
          <el-descriptions-item label="早退时间">{{ detailData.earlyLeaveTime }}</el-descriptions-item>
          <el-descriptions-item label="早退次数">{{ detailData.earlyLeaveCount }}</el-descriptions-item>
          <el-descriptions-item label="旷工时间">{{ detailData.absenteeismTime }}</el-descriptions-item>
          <el-descriptions-item label="旷工次数">{{ detailData.absenteeismCount }}</el-descriptions-item>
          <el-descriptions-item label="正班出勤时间">{{ detailData.regularAttendanceTime }}</el-descriptions-item>
          <el-descriptions-item label="员工确认">{{ detailData.employeeConfirmation }}</el-descriptions-item>
          <el-descriptions-item label="直接上级确认">{{ detailData.superiorConfirmation }}</el-descriptions-item>
          <el-descriptions-item label="部门经理确认">{{ detailData.managerConfirmation }}</el-descriptions-item>
          <el-descriptions-item label="考勤异常原因" :span="3">{{ detailData.abnormalReason }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAbnormalAttendanceList, getAbnormalAttendanceDetail, deleteAbnormalAttendance } from '@/api/abnormalAttendance';

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      detailData: null
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await getAbnormalAttendanceList({
          current: this.currentPage,
          size: this.pageSize
        });
        this.tableData = response.data.records;
        this.total = response.data.total;
      } catch (error) {
        this.$message.error('获取数据失败：' + error.message);
      }
    },
    async handleDetail(row) {
      try {
        const response = await getAbnormalAttendanceDetail(row.id);
        this.detailData = response.data;
        this.dialogVisible = true;
      } catch (error) {
        this.$message.error('获取详情失败：' + error.message);
      }
    },
    handleEdit(row) {
      this.$router.push(`/main/renshi/kaoqing/kqguanli/kqyichanggl?id=${row.id}`);
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该考勤记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        await deleteAbnormalAttendance(row.id);
        this.$message.success('删除成功');
        this.fetchData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message);
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    formatDate(date) {
      if (!date) return '';
      return date.split('T')[0];
    }
  }
};
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style>